package main.util
{
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class Util
	{
		
		public static function loadXML(url:String, callback:Function, remNameSpaces:Boolean=true):void {
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, function(e:Event):void {
				
				
				if (!remNameSpaces) {
					callback(new XML(ldr.data));
				} else {
					callback(removeNameSpaces(ldr.data));				
				}
			});
			ldr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(e:SecurityErrorEvent):void {
				throw new SecurityError("Security Error loading: "+url);
			});
			ldr.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void {
				throw new IOError("IO Error loading: "+url);
			});
			ldr.load(new URLRequest(url));
		}
		
		public static function removeNameSpaces(data:String):XML {
			
				var exp : RegExp = /xmlns=".*?" /g ;
			var result : String = data.replace ( exp , "" ) ;
			return new XML ( result ) ;
		}
		
		
		public static function exists(o:*):* {
			if (o == undefined) throw new Error("Does not exist");
			return o;
		}

		public static function rand(n:Number):Number {
			return (Math.random()*n)-(n/2);
		}

	}
}